package com.coffee.order.service.inquire.impl;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.merchant.query.MerchantInvoiceQuery;
import com.coffee.admin.api.supplier.query.SupplierQuery;
import com.coffee.admin.api.supplier.query.SupplierSystemDeployQuery;
import com.coffee.admin.service.common.bean.GtRegion;
import com.coffee.admin.service.common.dao.GtRegionDao;
import com.coffee.admin.service.merchant.bean.Merchant;
import com.coffee.admin.service.merchant.bean.MerchantCity;
import com.coffee.admin.service.merchant.bean.MerchantIntegralLog;
import com.coffee.admin.service.merchant.bean.MerchantInvoice;
import com.coffee.admin.service.merchant.dao.MerchantCityDao;
import com.coffee.admin.service.merchant.dao.MerchantDao;
import com.coffee.admin.service.merchant.dao.MerchantIntegralLogDao;
import com.coffee.admin.service.merchant.dao.MerchantInvoiceDao;
import com.coffee.admin.service.supplier.bean.Supplier;
import com.coffee.admin.service.supplier.bean.SupplierSystemDeploy;
import com.coffee.admin.service.supplier.dao.SupplierDao;
import com.coffee.admin.service.supplier.dao.SupplierSystemDeployDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.*;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.exception.ValidateBusinessException;
import com.coffee.core.util.*;
import com.coffee.order.api.inquire.query.*;
import com.coffee.order.api.inquire.req.*;
import com.coffee.order.api.inquire.result.*;
import com.coffee.order.api.inquire.service.InquireOrderService;
import com.coffee.order.api.sku.query.OrderRelationQuery;
import com.coffee.order.api.sku.query.SkuOrderDetailPeriodizationQuery;
import com.coffee.order.api.sku.query.SkuOrderDetailQuery;
import com.coffee.order.api.sku.query.SkuOrderQuery;
import com.coffee.order.api.sku.result.OrderResult;
import com.coffee.order.service.inquire.bean.*;
import com.coffee.order.service.inquire.dao.*;
import com.coffee.order.service.sku.bean.*;
import com.coffee.order.service.sku.dao.*;
import com.coffee.sku.api.inquire.query.InquireSkuTestingMessageQuery;
import com.coffee.sku.api.inquire.result.InquireSalesInformationResult;
import com.coffee.sku.api.inquire.result.InquireSkuContentResult;
import com.coffee.sku.api.inquire.result.InquireSkuTestingMessageResult;
import com.coffee.sku.service.inquire.bean.InquireSalesInformation;
import com.coffee.sku.service.inquire.bean.InquireSkuContent;
import com.coffee.sku.service.inquire.bean.InquireSkuTestingMessage;
import com.coffee.sku.service.inquire.dao.InquireSalesInformationDao;
import com.coffee.sku.service.inquire.dao.InquireSkuContentDao;
import com.coffee.sku.service.inquire.dao.InquireSkuTestingMessageDao;
import com.coffee.sku.service.sku.bean.Sku;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.bean.SkuContent;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import com.coffee.sku.service.sku.dao.SkuContentDao;
import com.coffee.sku.service.sku.dao.SkuDao;
import com.coffee.sku.service.sku.dao.SkuSalesInformationDao;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class InquireOrderServiceImpl implements InquireOrderService {
    private static final Logger LOG = LoggerFactory.getLogger(InquireOrderServiceImpl.class);
    @Resource
    private InquireOrderDao inquireOrderDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private MerchantDao merchantDao;
    @Resource
    private MerchantCityDao merchantCityDao;
    @Resource
    private MerchantInvoiceDao merchantInvoiceDao;
    @Resource
    private GtRegionDao gtRegionDao;
    @Resource
    private MerchantIntegralLogDao merchantIntegralLogDao;
    @Resource
    private InquireSkuContentDao inquireSkuContentDao;
    @Resource
    private InquireSkuTestingMessageDao inquireSkuTestingMessageDao;
    @Resource
    private InquireSalesInformationDao inquireSalesInformationDao;
    @Resource
    private InquireOrderPlanDao inquireOrderPlanDao;
    @Resource
    private InquireBuyerConfirmPlanDao inquireBuyerConfirmPlanDao;
    @Resource
    private InquireOrderContractDao inquireOrderContractDao;
    @Resource
    private InquireOrderTestingDao inquireOrderTestingDao;
    @Resource
    private InquireOrderDeliveryDemandDao inquireOrderDeliveryDemandDao;
    @Resource
    private SkuDao skuDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private PayOrderDao payOrderDao;
    @Resource
    private SkuOrderDao skuOrderDao;
    @Resource
    private OrderBalanceDao orderBalanceDao;
    @Resource
    private SkuContentDao skuContentDao;
    @Resource
    private SkuSalesInformationDao skuSalesInformationDao;
    @Resource
    private SkuOrderDetailDao skuOrderDetailDao;
    @Resource
    private PeriodizationSkuDao periodizationSkuDao;
    @Resource
    private SkuOrderDetailPeriodizationDao skuOrderDetailPeriodizationDao;
    @Resource
    private SkuOrderDeliveryDemandDao skuOrderDeliveryDemandDao;
    @Resource
    private InquireOrderLogDao inquireOrderLogDao;
    @Resource
    private SupplierDao supplierDao;
    @Resource
    private SupplierSystemDeployDao supplierSystemDeployDao;
    @Resource
    private OrderRelationDao orderRelationDao;

    @Value("${TAG}")
    private String environment;

    @Override
    public Page<InquireOrderResult> getPager(InquireOrderQuery param, PagerInfo pr) {
        Page<InquireOrderResult> pager = new Page<InquireOrderResult>(pr.getStart(), pr.getPageSize());
        Integer count = inquireOrderDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<InquireOrder> list = inquireOrderDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, InquireOrderResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(InquireOrderQuery param) {
        Integer count = inquireOrderDao.count(param);
        return count;
    }

    @Override
    public InquireOrderResult getDetail(Integer id) {
        InquireOrder item = inquireOrderDao.findById(id);
        if (item == null) {
            return null;
        }
        InquireOrderResult result = new InquireOrderResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<InquireOrderResult> getList(InquireOrderQuery param, PagerInfo pr) {
        List<InquireOrder> list = inquireOrderDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, InquireOrderResult.class);
    }

    @Override
    public List<InquireOrderResult> getList(InquireOrderQuery param) {
        List<InquireOrder> list = inquireOrderDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, InquireOrderResult.class);
    }

    @Override
    public List<InquireOrderResult> getList() {
        InquireOrderQuery param = new InquireOrderQuery();
        List<InquireOrder> list = inquireOrderDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, InquireOrderResult.class);
    }


    @Override
    public void delete(Integer id) {
        inquireOrderDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(InquireOrderResult result) throws BusinessException {
        InquireOrder item = new InquireOrder();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            inquireOrderDao.insert(item);
        } else {
            InquireOrder tmp = inquireOrderDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            inquireOrderDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, InquireOrderResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, InquireOrderResult> map = new HashMap<Integer, InquireOrderResult>();
        List<InquireOrder> list = inquireOrderDao.findByIds(StringUtil.setToList(idSet));
        List<InquireOrderResult> resultList = DTOUtils.convertList(list, InquireOrderResult.class);
        for (InquireOrderResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, InquireOrderResult> getItemMap() {
        Map<Integer, InquireOrderResult> map = new HashMap<Integer, InquireOrderResult>();
        InquireOrderQuery param = new InquireOrderQuery();
        List<InquireOrder> list = inquireOrderDao.page(param, 0, Integer.MAX_VALUE);
        List<InquireOrderResult> resultList = DTOUtils.convertList(list, InquireOrderResult.class);
        for (InquireOrderResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddInquireOrderReq req) throws BusinessException {
        ValidateBusinessException.assertNonNull(req.getMerchantCityId(), ExceptionEnum.ERROR_1843);
        ValidateBusinessException.assertNonNull(req.getCount(), ExceptionEnum.ERROR_1843);
        Merchant merchant = merchantDao.findById(req.getMerchantId());
        ValidateBusinessException.assertTrue(merchant.getBuyPermission().contains(SkuTypeEnum.INQUIRE.getCode()), ExceptionEnum.ERROR_1811);
        InquireSkuContentResult inquireSkuContentResult = inquireSkuContentDao.findBySkuId(req.getSkuId());
        ValidateBusinessException.assertNonNull(inquireSkuContentResult, ExceptionEnum.ERROR_1701);
        Sku sku = skuDao.findById(inquireSkuContentResult.getSkuId());
        ValidateBusinessException.assertNonNull(sku, ExceptionEnum.ERROR_1701);
        ValidateBusinessException.assertTrue("Y".equals(inquireSkuContentResult.getIsDisable()), ExceptionEnum.ERROR_1708);
        ValidateBusinessException.assertTrue("Y".equals(sku.getIsEnable()), ExceptionEnum.ERROR_1702);
        MerchantCity merchantCity = getMerchantCity(req.getMerchantCityId());
        Order param = new Order();
        param.setMerchantId(req.getMerchantId());
        param.setOrderType(SkuTypeEnum.INQUIRE.getCode());
        param.setCreateTime(new Date());
        param.setMerchantCityId(req.getMerchantCityId());
        if (merchantCity != null) {
            param.setReceiver(merchantCity.getReceiver());
            param.setReceiverMobile(merchantCity.getReceiverMobile());
            param.setAddress(merchantCity.getProvinceName() + merchantCity.getCityName() + merchantCity.getAreaName() + merchantCity.getAddress());
        }
        param.setSupplierId(sku.getSupplierId());
        orderDao.insert(param);
        InquireOrder inquireOrder = new InquireOrder();
        inquireOrder.setOrderId(param.getId());
        inquireOrder.setSupplierId(inquireSkuContentResult.getSupplierId());
        inquireOrder.setSkuId(sku.getId());
        inquireOrder.setInquireSalesInformationId(req.getInquireSalesInformationId());
        inquireOrder.setMerchantId(req.getMerchantId());
        inquireOrder.setInquireSkuId(req.getInquireSkuId());
        inquireOrder.setStatus(InquireOrderStatusEnum.WAITING.getCode());
        inquireOrder.setCount(req.getCount());
        inquireOrder.setType(OrderTypeEnum.INQUIRE.getCode());
        inquireOrder.setDeliveryMode(inquireSkuContentResult.getLogistics());
        inquireOrder.setCreateTime(new Date());
        inquireOrder.setRemarks(req.getRemarks());
        inquireOrderDao.insert(inquireOrder);
        addInquireOrderLog(param.getId(), "期货询价", "买家", merchant.getName());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WeiXinPushResult addQuotationScheme(SectionProgrammeReq req) throws BusinessException {
        Supplier supplier = supplierDao.findById(req.getSupplierId());
        ValidateBusinessException.assertIdNotNull(req.getInquireOrderId(), ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(req.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertFalse(InquireOrderStatusEnum.CANCEL.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1847);
        List<SectionProgrammeListReq> sectionProgrammeListReqs = req.getSectionProgrammeListReqs();
        List<ExplicitProgrammeListReq> explicitProgrammeListReqs = req.getExplicitProgrammeListReqs();
        ValidateBusinessException.assertFalse(sectionProgrammeListReqs == null && explicitProgrammeListReqs == null, ExceptionEnum.ERROR_1837);
        if (sectionProgrammeListReqs != null && sectionProgrammeListReqs.size() > 0) {
            addSectionProgrammeList(req.getInquireOrderId(), sectionProgrammeListReqs);
            addInquireOrderLog(inquireOrder.getOrderId(), "意向报价反馈", "卖方", supplier.getName());
        }
        InquireOrder updateInquireOrder = new InquireOrder();
        updateInquireOrder.setId(req.getInquireOrderId());
        updateInquireOrder.setStatus(InquireOrderStatusEnum.WAITINGCONFIRM.getCode());
        updateInquireOrder.setExplicitTime(new Date());
        inquireOrderDao.updateById(updateInquireOrder);
        if (explicitProgrammeListReqs != null && explicitProgrammeListReqs.size() > 0) {
            addExplicitProgrammeList(req.getInquireOrderId(), explicitProgrammeListReqs);
            addInquireOrderLog(inquireOrder.getOrderId(), "明确报价反馈", "卖方", supplier.getName());
        }
        WeiXinPushResult weiXinPushResult = new WeiXinPushResult();
        Merchant merchant = merchantDao.findById(inquireOrder.getMerchantId());
        Sku sku = skuDao.findById(inquireOrder.getSkuId());
        weiXinPushResult.setOpenId(merchant.getOpenId());
        weiXinPushResult.setInquireOrderId(inquireOrder.getId());
        weiXinPushResult.setSkuName(sku.getTitle());
        weiXinPushResult.setStatus(InquireOrderStatusEnum.WAITINGCONFIRM.getName());
        weiXinPushResult.setCreateTime(new Date());
        return weiXinPushResult;
    }


    void addInquireOrderLog(Integer orderId, String describe, String describePeople, String platformName) {
        InquireOrderLog inquireOrderLog = new InquireOrderLog();
        inquireOrderLog.setOrderId(orderId);
        inquireOrderLog.setDescribe(describe);
        inquireOrderLog.setDescribePeople(describePeople);
        inquireOrderLog.setPlatformName(platformName);
        inquireOrderLog.setCreateTime(new Date());
        inquireOrderLog.setType(OrderTypeEnum.INQUIRE.getCode());
        inquireOrderLogDao.insert(inquireOrderLog);
    }


    void addSectionProgrammeList(Integer inquireOrderId, List<SectionProgrammeListReq> sectionProgrammeListReqs) throws BusinessException {
        InquireOrderPlan inquireOrderPlan = null;
        InquireOrderPlanQuery inquireOrderPlanQuery = new InquireOrderPlanQuery();
        inquireOrderPlanQuery.setInquireOrderId(inquireOrderId);
        inquireOrderPlanQuery.setType(InquireOrderPlanTypeEnum.INTENTION.getCode());
        List<InquireOrderPlan> inquireOrderPlans = inquireOrderPlanDao.list(inquireOrderPlanQuery);
        if (inquireOrderPlans != null && inquireOrderPlans.size() > 0) {
            return;
        }
        for (SectionProgrammeListReq sectionProgrammeListReq : sectionProgrammeListReqs) {
            inquireOrderPlan = new InquireOrderPlan();
            inquireOrderPlan.setInquireOrderId(inquireOrderId);
            inquireOrderPlan.setName(sectionProgrammeListReq.getName());
            inquireOrderPlan.setSpecs(sectionProgrammeListReq.getSpecs());
            inquireOrderPlan.setUnit(sectionProgrammeListReq.getUnit());
            inquireOrderPlan.setType(sectionProgrammeListReq.getType());
            inquireOrderPlan.setPriceSection(sectionProgrammeListReq.getPriceSection());
            inquireOrderPlan.setFactory(sectionProgrammeListReq.getFactory());
            inquireOrderPlan.setStock(sectionProgrammeListReq.getStock());
            inquireOrderPlan.setCreateTime(new Date());
            inquireOrderPlanDao.insert(inquireOrderPlan);
        }

    }


    void addExplicitProgrammeList(Integer inquireOrderId, List<ExplicitProgrammeListReq> explicitProgrammeListReqs) throws BusinessException {
        InquireOrderPlanQuery inquireOrderPlanQuery = new InquireOrderPlanQuery();
        inquireOrderPlanQuery.setInquireOrderId(inquireOrderId);
        inquireOrderPlanQuery.setType(InquireOrderPlanTypeEnum.EXPLICIT.getCode());
        List<InquireOrderPlan> inquireOrderPlans = inquireOrderPlanDao.list(inquireOrderPlanQuery);
        Map<Integer, InquireOrderPlan> inquireOrderPlanMap = new HashMap<>();
        if (inquireOrderPlans != null && inquireOrderPlans.size() > 0) {
            inquireOrderPlanMap = inquireOrderPlans.stream().collect(Collectors.toMap(InquireOrderPlan::getId, i -> i));
        }
        InquireOrderPlan inquireOrderPlan = null;
        for (ExplicitProgrammeListReq explicitProgrammeListReq : explicitProgrammeListReqs) {
            if (explicitProgrammeListReq.getId() != null) {
                if (explicitProgrammeListReq.getPrice() == null) {
                    throw new BusinessException(ExceptionEnum.ERROR_1833.getCode(), ExceptionEnum.ERROR_1833.getMessage());
                }
                InquireOrderPlan orderPlan = inquireOrderPlanMap.get(explicitProgrammeListReq.getId());
                if (orderPlan.getPrice() != null) {
                    throw new BusinessException(ExceptionEnum.ERROR_1848.getCode(), ExceptionEnum.ERROR_1848.getMessage());
                }
                ValidateBusinessException.assertNonNull(orderPlan, ExceptionEnum.ERROR_1819);
                InquireOrderPlan updatePlan = new InquireOrderPlan();
                updatePlan.setId(orderPlan.getId());
                updatePlan.setPrice(explicitProgrammeListReq.getPrice());
                updatePlan.setStock(explicitProgrammeListReq.getStock());
                inquireOrderPlanDao.updateById(updatePlan);
            } else if (inquireOrderPlans != null && inquireOrderPlans.size() > 0) {
                throw new BusinessException(ExceptionEnum.ERROR_1819.getCode(), ExceptionEnum.ERROR_1819.getMessage());
            } else {
                if (explicitProgrammeListReq.getPrice() == null) {
                    throw new BusinessException(ExceptionEnum.ERROR_1833.getCode(), ExceptionEnum.ERROR_1833.getMessage());
                }
                inquireOrderPlan = new InquireOrderPlan();
                inquireOrderPlan.setInquireOrderId(inquireOrderId);
                inquireOrderPlan.setName(explicitProgrammeListReq.getName());
                inquireOrderPlan.setSpecs(explicitProgrammeListReq.getSpecs());
                inquireOrderPlan.setUnit(explicitProgrammeListReq.getUnit());
                inquireOrderPlan.setType(explicitProgrammeListReq.getType());
                inquireOrderPlan.setPrice(explicitProgrammeListReq.getPrice());
                inquireOrderPlan.setFactory(explicitProgrammeListReq.getFactory());
                inquireOrderPlan.setStock(explicitProgrammeListReq.getStock());
                inquireOrderPlan.setCreateTime(new Date());
                inquireOrderPlanDao.insert(inquireOrderPlan);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void buyerConfirm(BuyerConfirmReq req) throws BusinessException {
        Merchant merchant = merchantDao.findById(req.getMerchantId());
        ValidateBusinessException.assertIdNotNull(req.getInquireOrderId(), ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(req.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertTrue(req.getMerchantId().equals(inquireOrder.getMerchantId()), ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertFalse(InquireOrderStatusEnum.CANCEL.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1847);
        InquireBuyerConfirmPlanQuery inquireBuyerConfirmPlanQuery = new InquireBuyerConfirmPlanQuery();
        inquireBuyerConfirmPlanQuery.setInquireOrderId(inquireOrder.getId());
        InquireBuyerConfirmPlan inquireBuyerConfirmPlanDaoFirst = inquireBuyerConfirmPlanDao.findFirst(inquireBuyerConfirmPlanQuery);
        ValidateBusinessException.assertNull(inquireBuyerConfirmPlanDaoFirst, ExceptionEnum.ERROR_1844);
        InquireOrderPlanQuery inquireOrderPlanQuery = new InquireOrderPlanQuery();
        inquireOrderPlanQuery.setInquireOrderId(inquireOrder.getId());
        inquireOrderPlanQuery.setType(InquireOrderPlanTypeEnum.EXPLICIT.getCode());
        List<InquireOrderPlan> inquireOrderPlans = inquireOrderPlanDao.list(inquireOrderPlanQuery);
        List<Integer> inquireOrderPlansId = inquireOrderPlans.stream().map(InquireOrderPlan::getId).collect(Collectors.toList());
        Map<Integer, InquireOrderPlan> inquireOrderPlanMap = inquireOrderPlans.stream().collect(Collectors.toMap(InquireOrderPlan::getId, i -> i));
        List<BuyerConfirmListReq> buyerConfirmListReqs = req.getBuyerConfirmListReqs();
        List<Integer> buyerConfirmListId = buyerConfirmListReqs.stream().map(BuyerConfirmListReq::getInquireOrderPlanId).collect(Collectors.toList());
        ValidateBusinessException.assertTrue(inquireOrderPlansId.containsAll(buyerConfirmListId), ExceptionEnum.ERROR_1814);
        Double totalPrice = 0.0;
        Integer count = 0;
        for (BuyerConfirmListReq buyerConfirmListReq : buyerConfirmListReqs) {
            InquireOrderPlan inquireOrderPlan = inquireOrderPlanMap.get(buyerConfirmListReq.getInquireOrderPlanId());
            if (inquireOrderPlan.getPrice() == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1845.getCode(), inquireOrderPlan.getName() + ExceptionEnum.ERROR_1845.getMessage());
            }
            if (buyerConfirmListReq.getCount() > inquireOrderPlan.getStock()) {
                throw new BusinessException(ExceptionEnum.ERROR_1816.getCode(), inquireOrderPlan.getName() + ExceptionEnum.ERROR_1816.getMessage());
            }
            InquireBuyerConfirmPlan inquireBuyerConfirmPlan = new InquireBuyerConfirmPlan();
            inquireBuyerConfirmPlan.setInquireOrderId(req.getInquireOrderId());
            inquireBuyerConfirmPlan.setInquireOrderPlanId(buyerConfirmListReq.getInquireOrderPlanId());
            inquireBuyerConfirmPlan.setCount(buyerConfirmListReq.getCount());
            double doubleValue = new BigDecimal(inquireOrderPlan.getPrice() + "").multiply(new BigDecimal(buyerConfirmListReq.getCount())).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
            totalPrice += doubleValue;
            count += buyerConfirmListReq.getCount();
            inquireBuyerConfirmPlan.setTotalPrice(doubleValue);
            inquireBuyerConfirmPlan.setCreateTime(new Date());
            inquireBuyerConfirmPlanDao.insert(inquireBuyerConfirmPlan);
        }
        InquireOrder param = new InquireOrder();
        param.setId(req.getInquireOrderId());
        param.setTotalPrice(totalPrice);
        param.setCount(count);
        param.setStatus(InquireOrderStatusEnum.BUYERGCONFIRM.getCode());
        param.setBuyerConfirmTime(new Date());
        inquireOrderDao.updateById(param);
        addInquireOrderLog(inquireOrder.getOrderId(), "买家确认", "买家", merchant.getName());
    }

    @Override
    public void buyerIntentionConfirm(BuyerIntentionConfirmReq req) throws BusinessException {
        Merchant merchant = merchantDao.findById(req.getMerchantId());
        ValidateBusinessException.assertIdNotNull(req.getInquireOrderId(), ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(req.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertTrue(req.getMerchantId().equals(inquireOrder.getMerchantId()), ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertFalse(InquireOrderStatusEnum.CANCEL.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1847);
        InquireOrderPlanQuery explicitInquireOrderPlanQuery = new InquireOrderPlanQuery();
        explicitInquireOrderPlanQuery.setInquireOrderId(inquireOrder.getId());
        explicitInquireOrderPlanQuery.setType(InquireOrderPlanTypeEnum.EXPLICIT.getCode());
        InquireOrderPlan inquireOrderPlanDaoFirst = inquireOrderPlanDao.findFirst(explicitInquireOrderPlanQuery);
        ValidateBusinessException.assertNull(inquireOrderPlanDaoFirst, ExceptionEnum.ERROR_1819);
        InquireOrderPlanQuery inquireOrderPlanQuery = new InquireOrderPlanQuery();
        inquireOrderPlanQuery.setInquireOrderId(inquireOrder.getId());
        inquireOrderPlanQuery.setType(InquireOrderPlanTypeEnum.INTENTION.getCode());
        List<InquireOrderPlan> inquireOrderPlans = inquireOrderPlanDao.findByInquireOrderId(req.getInquireOrderId());
        List<Integer> inquireOrderPlansId = inquireOrderPlans.stream().map(InquireOrderPlan::getId).collect(Collectors.toList());
        Map<Integer, InquireOrderPlan> inquireOrderPlanMap = inquireOrderPlans.stream().collect(Collectors.toMap(InquireOrderPlan::getId, i -> i));
        ValidateBusinessException.assertTrue(inquireOrderPlansId.containsAll(req.getIntegerList()), ExceptionEnum.ERROR_1814);
        for (Integer integer : req.getIntegerList()) {
            InquireOrderPlan inquireOrderPlan = inquireOrderPlanMap.get(integer);
            InquireOrderPlan insertPaln = new InquireOrderPlan();
            insertPaln.setInquireOrderId(req.getInquireOrderId());
            insertPaln.setName(inquireOrderPlan.getName());
            insertPaln.setSpecs(inquireOrderPlan.getSpecs());
            insertPaln.setUnit(inquireOrderPlan.getUnit());
            insertPaln.setType(InquireOrderPlanTypeEnum.EXPLICIT.getCode());
            insertPaln.setPriceSection(inquireOrderPlan.getPriceSection());
            insertPaln.setFactory(inquireOrderPlan.getFactory());
            insertPaln.setStock(inquireOrderPlan.getStock());
            insertPaln.setCreateTime(new Date());
            inquireOrderPlanDao.insert(insertPaln);
        }
        addInquireOrderLog(inquireOrder.getOrderId(), "买家选择意向报价", "买家", merchant.getName());
    }

    @Override
    public WeiXinPushResult sellerConfirm(SellerConfirmReq req) throws BusinessException {
        Supplier supplier = supplierDao.findById(req.getUserId());
        ValidateBusinessException.assertIdNotNull(req.getInquireOrderId(), ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(req.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertFalse(InquireOrderStatusEnum.CANCEL.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1847);
        ValidateBusinessException.assertTrue(InquireOrderStatusEnum.BUYERGCONFIRM.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1820);
        InquireOrder param = new InquireOrder();
        param.setId(req.getInquireOrderId());
        param.setStatus(InquireOrderStatusEnum.SELLERCONFIRM.getCode());
        param.setSellerConfirmTime(new Date());
        inquireOrderDao.updateById(param);
        addInquireOrderLog(inquireOrder.getOrderId(), "卖方确认", "卖方", supplier.getName());
        WeiXinPushResult weiXinPushResult = new WeiXinPushResult();
        Merchant merchant = merchantDao.findById(inquireOrder.getMerchantId());
        Sku sku = skuDao.findById(inquireOrder.getSkuId());
        weiXinPushResult.setOpenId(merchant.getOpenId());
        weiXinPushResult.setInquireOrderId(inquireOrder.getId());
        weiXinPushResult.setSkuName(sku.getTitle());
        weiXinPushResult.setStatus(InquireOrderStatusEnum.SELLERCONFIRM.getName());
        weiXinPushResult.setCreateTime(new Date());
        return weiXinPushResult;
    }

    @Override
    public WeiXinPushResult pay(SellerConfirmReq req) throws BusinessException {
        ValidateBusinessException.assertIdNotNull(req.getInquireOrderId(), ExceptionEnum.ERROR_1812);
        ValidateBusinessException.assertNonNull(req.getCover(), ExceptionEnum.ERROR_1828);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(req.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertTrue(InquireOrderStatusEnum.SELLERCONFIRM.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1821);
        ValidateBusinessException.assertFalse(InquireOrderStatusEnum.CANCEL.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1847);
        InquireSkuContent inquireSkuContent = inquireSkuContentDao.findById(inquireOrder.getInquireSkuId());
        InquireOrder param = new InquireOrder();
        param.setId(req.getInquireOrderId());
        param.setStatus(InquireOrderStatusEnum.PAY.getCode());
        param.setPayTime(new Date());
        inquireOrderDao.updateById(param);
        String cover = req.getCover();
        InquireOrderContract inquireOrderContract = null;
        for (String s : cover.split(";")) {
            inquireOrderContract = new InquireOrderContract();
            inquireOrderContract.setInquireOrderId(req.getInquireOrderId());
            inquireOrderContract.setType(ContractTypeEnum.PAYING.getCode());
            inquireOrderContract.setCover(s);
            inquireOrderContract.setCreateTime(new Date());
            inquireOrderContractDao.insert(inquireOrderContract);
        }
        InquireOrderDeliveryDemand inquireOrderDeliveryDemand = new InquireOrderDeliveryDemand();
        inquireOrderDeliveryDemand.setInquireOrderId(req.getInquireOrderId());
        inquireOrderDeliveryDemand.setDeliveryMode(inquireSkuContent.getLogistics());
        inquireOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.WAITING.getCode());
        inquireOrderDeliveryDemand.setCreateTime(new Date());
        inquireOrderDeliveryDemand.setMerchantId(inquireOrder.getMerchantId());
        inquireOrderDeliveryDemandDao.insert(inquireOrderDeliveryDemand);
        if (RoleTypeEnum.MERCHANT.getCode().equals(req.getType())) {
            Merchant merchant = merchantDao.findById(req.getUserId());
            addInquireOrderLog(inquireOrder.getOrderId(), "付款", "买家", merchant.getName());
        } else if (RoleTypeEnum.SUPPLIER.getCode().equals(req.getType())) {
            Supplier supplier = supplierDao.findById(req.getUserId());
            addInquireOrderLog(inquireOrder.getOrderId(), "付款", "卖方", supplier.getName());
        }
        WeiXinPushResult weiXinPushResult = new WeiXinPushResult();
        Merchant merchant = merchantDao.findById(inquireOrder.getMerchantId());
        Sku sku = skuDao.findById(inquireOrder.getSkuId());
        weiXinPushResult.setOpenId(merchant.getOpenId());
        weiXinPushResult.setInquireOrderId(inquireOrder.getId());
        weiXinPushResult.setSkuName(sku.getTitle());
        weiXinPushResult.setStatus(InquireOrderStatusEnum.PAY.getName());
        weiXinPushResult.setCreateTime(new Date());
        return weiXinPushResult;

    }

    @Override
    public void contract(SellerConfirmReq req) throws BusinessException {
        ValidateBusinessException.assertIdNotNull(req.getInquireOrderId(), ExceptionEnum.ERROR_1812);
        ValidateBusinessException.assertNonNull(req.getCover(), ExceptionEnum.ERROR_1828);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(req.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertFalse(InquireOrderStatusEnum.CANCEL.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1847);
        String cover = req.getCover();
        InquireOrderContract inquireOrderContract = null;
        for (String s : cover.split(";")) {
            inquireOrderContract = new InquireOrderContract();
            inquireOrderContract.setInquireOrderId(req.getInquireOrderId());
            inquireOrderContract.setType(ContractTypeEnum.CONTRACT.getCode());
            inquireOrderContract.setCover(s);
            inquireOrderContract.setCreateTime(new Date());
            inquireOrderContractDao.insert(inquireOrderContract);
        }
        if (RoleTypeEnum.MERCHANT.getCode().equals(req.getType())) {
            Merchant merchant = merchantDao.findById(req.getUserId());
            addInquireOrderLog(inquireOrder.getOrderId(), "上传签约凭证", "买家", merchant.getName());
        } else if (RoleTypeEnum.SUPPLIER.getCode().equals(req.getType())) {
            Supplier supplier = supplierDao.findById(req.getUserId());
            addInquireOrderLog(inquireOrder.getOrderId(), "上传签约凭证", "卖方", supplier.getName());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WeiXinPushResult setFreight(InquireOrderDeliveryDemandReq req) throws BusinessException {
        ValidateBusinessException.assertIdNotNull(req.getInquireOrderId(), ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(req.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        ValidateBusinessException.assertTrue(InquireOrderStatusEnum.PAY.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1822);
        ValidateBusinessException.assertFalse(InquireOrderStatusEnum.CANCEL.getCode().equals(inquireOrder.getStatus()), ExceptionEnum.ERROR_1847);
        InquireOrderDeliveryDemand inquireOrderDeliveryDemand = inquireOrderDeliveryDemandDao.findById(req.getId());
        ValidateBusinessException.assertNonNull(inquireOrderDeliveryDemand, ExceptionEnum.ERROR_1802);
        ValidateBusinessException.assertTrue(DeliveryWayEnum.PAYMENT.getCode().equals(inquireOrderDeliveryDemand.getDeliveryMode()), ExceptionEnum.ERROR_1810);
        boolean b = SkuOrderStatusEnum.WAITING.getCode().equals(inquireOrderDeliveryDemand.getPostageStatus()) || StringUtils.isEmpty(inquireOrderDeliveryDemand.getPostageStatus());
        ValidateBusinessException.assertTrue(b, ExceptionEnum.ERROR_1807);
        InquireOrderDeliveryDemand updateSkuOrderDeliveryDemand = new InquireOrderDeliveryDemand();
        updateSkuOrderDeliveryDemand.setId(req.getId());
        updateSkuOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.WAITING.getCode());
        updateSkuOrderDeliveryDemand.setLogisticsName(req.getLogisticsName());
        updateSkuOrderDeliveryDemand.setPostage(req.getPostage());
        inquireOrderDeliveryDemandDao.updateById(updateSkuOrderDeliveryDemand);
        InquireOrder param = new InquireOrder();
        param.setId(req.getInquireOrderId());
        param.setStatus(InquireOrderStatusEnum.WAITDELIVER.getCode());
        inquireOrderDao.updateById(param);
        Supplier supplier = supplierDao.findById(req.getUserId());
        addInquireOrderLog(inquireOrder.getOrderId(), "检测入库", "卖家", supplier.getName());
        WeiXinPushResult weiXinPushResult = new WeiXinPushResult();
        Merchant merchant = merchantDao.findById(inquireOrder.getMerchantId());
        Sku sku = skuDao.findById(inquireOrder.getSkuId());
        weiXinPushResult.setOpenId(merchant.getOpenId());
        weiXinPushResult.setInquireOrderId(inquireOrder.getId());
        weiXinPushResult.setSkuName(sku.getTitle());
        weiXinPushResult.setStatus(InquireOrderStatusEnum.WAITDELIVER.getName());
        weiXinPushResult.setCreateTime(new Date());
        return weiXinPushResult;
    }

    @Override
    public OrderResult logisticsPayByInquire(Integer merchantId, Integer inquireOrderDeliveryDemandId) throws BusinessException {
        OrderResult orderResult = new OrderResult();
        InquireOrderDeliveryDemand inquireOrderDeliveryDemand = inquireOrderDeliveryDemandDao.findByIdAndMerchantId(inquireOrderDeliveryDemandId, merchantId);
        ValidateBusinessException.assertNonNull(inquireOrderDeliveryDemand, ExceptionEnum.ERROR_1801);
        ValidateBusinessException.assertNonNull(inquireOrderDeliveryDemand.getPostage(), ExceptionEnum.ERROR_1846);
        ValidateBusinessException.assertFalse(SkuOrderStatusEnum.PAY.getCode().equals(inquireOrderDeliveryDemand.getPostageStatus()), ExceptionEnum.ERROR_1840);
        orderResult.setOutOrderId(environment + OrderTypeEnum.INQUIRELOGISTICS.getCode() + "-" + inquireOrderDeliveryDemand.getId());
        orderResult.setOrderType(OrderTypeEnum.INQUIRELOGISTICS.getCode());
        orderResult.setTotalPrice(inquireOrderDeliveryDemand.getPostage());
        orderResult.setPayPrice(inquireOrderDeliveryDemand.getPostage());
        return orderResult;
    }

    @Override
    public WeiXinPushResult setDeliveryDemand(InquireOrderDeliveryDemandReq inquireOrderDeliveryDemandReq) throws BusinessException {
        ValidateBusinessException.assertIdNotNull(inquireOrderDeliveryDemandReq.getInquireOrderId(), ExceptionEnum.ERROR_1808);
        InquireOrder inquireOrder = inquireOrderDao.findById(inquireOrderDeliveryDemandReq.getInquireOrderId());
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1801);
        InquireOrderDeliveryDemand inquireOrderDeliveryDemand = inquireOrderDeliveryDemandDao.findById(inquireOrderDeliveryDemandReq.getId());
        ValidateBusinessException.assertNonNull(inquireOrderDeliveryDemand, ExceptionEnum.ERROR_1802);
        ValidateBusinessException.assertFalse(SkuOrderStatusEnum.SUCCESS.getCode().equals(inquireOrderDeliveryDemand.getPostageStatus()), ExceptionEnum.ERROR_1834);
        boolean b = DeliveryWayEnum.PAYMENT.getCode().equals(inquireOrderDeliveryDemand.getDeliveryMode()) && SkuOrderStatusEnum.WAITING.getCode().equals(inquireOrderDeliveryDemand.getPostageStatus());
        ValidateBusinessException.assertFalse(b, ExceptionEnum.ERROR_1835);
        InquireOrderDeliveryDemand updateSkuOrderDeliveryDemand = new InquireOrderDeliveryDemand();
        updateSkuOrderDeliveryDemand.setId(inquireOrderDeliveryDemand.getId());
        updateSkuOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.SUCCESS.getCode());
        updateSkuOrderDeliveryDemand.setLogisticsName(inquireOrderDeliveryDemandReq.getLogisticsName());
        updateSkuOrderDeliveryDemand.setLogisticsNumber(inquireOrderDeliveryDemandReq.getLogisticsNumber());
        inquireOrderDeliveryDemandDao.updateById(updateSkuOrderDeliveryDemand);
        InquireOrder param = new InquireOrder();
        param.setId(inquireOrderDeliveryDemandReq.getInquireOrderId());
        param.setStatus(InquireOrderStatusEnum.DELIVER.getCode());
        param.setDeliveryTime(new Date());
        inquireOrderDao.updateById(param);
        Supplier supplier = supplierDao.findById(inquireOrderDeliveryDemandReq.getUserId());
        addInquireOrderLog(inquireOrder.getOrderId(), "期货发货", "卖家", supplier.getName());
        WeiXinPushResult weiXinPushResult = new WeiXinPushResult();
        Merchant merchant = merchantDao.findById(inquireOrder.getMerchantId());
        Sku sku = skuDao.findById(inquireOrder.getSkuId());
        weiXinPushResult.setOpenId(merchant.getOpenId());
        weiXinPushResult.setInquireOrderId(inquireOrder.getId());
        weiXinPushResult.setSkuName(sku.getTitle());
        weiXinPushResult.setStatus(InquireOrderStatusEnum.WAITDELIVER.getName());
        weiXinPushResult.setCreateTime(new Date());
        return weiXinPushResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sampleOrder(SampleOrderReq sampleOrderReq) throws BusinessException {
        Integer inquireOrderId = sampleOrderReq.getInquireOrderId();
        String logisticsName = sampleOrderReq.getLogisticsName();
        String logisticsNumber = sampleOrderReq.getLogisticsNumber();
        Double postage = sampleOrderReq.getPostage();
        String type = sampleOrderReq.getType();
        Integer userId = sampleOrderReq.getUserId();
        Integer supplierId = sampleOrderReq.getSupplierId();
        ValidateBusinessException.assertIdNotNull(inquireOrderId, ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(inquireOrderId);
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        if (!((RoleTypeEnum.MERCHANT.getCode().equals(type) && inquireOrder.getMerchantId().equals(userId)) ||
                (RoleTypeEnum.SUPPLIER.getCode().equals(type) && inquireOrder.getSupplierId().equals(supplierId)))
        ) {
            throw new BusinessException(ExceptionEnum.ERROR_1823.getCode(), ExceptionEnum.ERROR_1823.getMessage());
        }
        InquireSkuContent inquireSkuContent = inquireSkuContentDao.findById(inquireOrder.getInquireSkuId());
        InquireOrder insertInquireOrder = new InquireOrder();
        insertInquireOrder.setOrderId(inquireOrder.getOrderId());
        insertInquireOrder.setSupplierId(inquireOrder.getSupplierId());
        insertInquireOrder.setMerchantId(inquireOrder.getMerchantId());
        insertInquireOrder.setInquireSkuId(inquireOrder.getInquireSkuId());
        insertInquireOrder.setTotalPrice(0.0);
        insertInquireOrder.setStatus(SkuOrderStatusEnum.PAY.getCode());
        insertInquireOrder.setCount(1);
        insertInquireOrder.setType(OrderTypeEnum.SAMPLE.getCode());
        insertInquireOrder.setDeliveryMode(inquireSkuContent.getSampleLogistics());
        insertInquireOrder.setPayTime(new Date());
        insertInquireOrder.setCreateTime(new Date());
        insertInquireOrder.setSkuId(inquireOrder.getSkuId());
        insertInquireOrder.setInquireSalesInformationId(inquireOrder.getInquireSalesInformationId());
        insertInquireOrder.setRemarks(sampleOrderReq.getRemarks());
        insertInquireOrder.setOutOrderId(sampleOrderReq.getOutOrderId());
        inquireOrderDao.insert(insertInquireOrder);
        Supplier supplier = supplierDao.findById(userId);
        addInquireOrderLog(inquireOrder.getOrderId(), "创建样品订单", "卖方", supplier.getName());
        InquireOrderDeliveryDemand inquireOrderDeliveryDemand = new InquireOrderDeliveryDemand();
        inquireOrderDeliveryDemand.setInquireOrderId(insertInquireOrder.getId());
        inquireOrderDeliveryDemand.setDeliveryMode(inquireSkuContent.getSampleLogistics());
        inquireOrderDeliveryDemand.setLogisticsName(logisticsName);
        inquireOrderDeliveryDemand.setLogisticsNumber(logisticsNumber);
        if (DeliveryWayEnum.PAYMENT.getCode().equals(inquireSkuContent.getSampleLogistics())) {
            inquireOrderDeliveryDemand.setPostage(postage);
            inquireOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.WAITING.getCode());
        } else {
            inquireOrderDeliveryDemand.setPostage(0.0);
            inquireOrderDeliveryDemand.setPostagePayTime(new Date());
            inquireOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.DELIVER.getCode());
        }
        inquireOrderDeliveryDemandDao.insert(inquireOrderDeliveryDemand);
        InquireOrder param = new InquireOrder();
        param.setId(insertInquireOrder.getId());
        param.setStatus(InquireOrderStatusEnum.DELIVER.getCode());
        param.setDeliveryTime(new Date());
        inquireOrderDao.updateById(param);

    }

    @Override
    public void sampleOrderByBuyer(SampleOrderReq sampleOrderReq) throws BusinessException {
        Integer inquireOrderId = sampleOrderReq.getInquireOrderId();
        Integer userId = sampleOrderReq.getUserId();
        ValidateBusinessException.assertIdNotNull(inquireOrderId, ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(inquireOrderId);
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        InquireSkuContent inquireSkuContent = inquireSkuContentDao.findById(inquireOrder.getInquireSkuId());
        InquireOrder insertInquireOrder = new InquireOrder();
        insertInquireOrder.setOrderId(inquireOrder.getOrderId());
        insertInquireOrder.setSupplierId(inquireOrder.getSupplierId());
        insertInquireOrder.setMerchantId(inquireOrder.getMerchantId());
        insertInquireOrder.setInquireSkuId(inquireOrder.getInquireSkuId());
        insertInquireOrder.setTotalPrice(0.0);
        insertInquireOrder.setStatus(SkuOrderStatusEnum.PAY.getCode());
        insertInquireOrder.setCount(1);
        insertInquireOrder.setType(OrderTypeEnum.SAMPLE.getCode());
        insertInquireOrder.setDeliveryMode(inquireSkuContent.getSampleLogistics());
        insertInquireOrder.setPayTime(new Date());
        insertInquireOrder.setCreateTime(new Date());
        insertInquireOrder.setSkuId(inquireOrder.getSkuId());
        insertInquireOrder.setInquireSalesInformationId(inquireOrder.getInquireSalesInformationId());
        insertInquireOrder.setRemarks(sampleOrderReq.getRemarks());
        insertInquireOrder.setOutOrderId(sampleOrderReq.getOutOrderId());
        inquireOrderDao.insert(insertInquireOrder);
        Merchant merchant = merchantDao.findById(userId);
        addInquireOrderLog(inquireOrder.getOrderId(), "创建样品订单", "买家", merchant.getName());
        InquireOrderDeliveryDemand inquireOrderDeliveryDemand = new InquireOrderDeliveryDemand();
        inquireOrderDeliveryDemand.setInquireOrderId(insertInquireOrder.getId());
        inquireOrderDeliveryDemand.setDeliveryMode(inquireSkuContent.getSampleLogistics());
        if (DeliveryWayEnum.PAYMENT.getCode().equals(inquireSkuContent.getSampleLogistics())) {
            inquireOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.WAITING.getCode());
        } else {
            inquireOrderDeliveryDemand.setPostage(0.0);
            inquireOrderDeliveryDemand.setPostagePayTime(new Date());
            inquireOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.DELIVER.getCode());
        }
        inquireOrderDeliveryDemandDao.insert(inquireOrderDeliveryDemand);
    }

    @Override
    public Page<OrderListResult> getPage(Integer merchantId, String status, PagerInfo pagerInfo) {
        Page<OrderListResult> page = new Page<>(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
        Integer listByMerchantCount = inquireOrderDao.findListByMerchantCount(merchantId, status);
        page.setTotalPage(listByMerchantCount);
        if (listByMerchantCount > 0) {
            List<OrderListResult> orderListResults = inquireOrderDao.findListByMerchant(merchantId, status, pagerInfo.getStart(), pagerInfo.getPageSize());
            for (OrderListResult orderListResult : orderListResults) {
                InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
                inquireOrderQuery.setOrderId(orderListResult.getId());
                List<InquireOrder> inquireOrders = inquireOrderDao.list(inquireOrderQuery);
                List<InquireOrderListResult> inquireOrderListResults = DTOUtils.convertList(inquireOrders, InquireOrderListResult.class);
                for (InquireOrderListResult inquireOrderListResult : inquireOrderListResults) {
                    if (OrderTypeEnum.INQUIRE.getCode().equals(inquireOrderListResult.getType())) {
                        orderListResult.setStatus(inquireOrderListResult.getStatus());
                    }
                    SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(inquireOrderListResult.getSupplierId(), OrderTypeEnum.INQUIRE.getCode(), inquireOrderListResult.getStatus());
                    if (supplierSystemDeploy != null) {
                        if (InquireOrderStatusEnum.WAITINGCONFIRM.getCode().equals(inquireOrderListResult.getStatus())) {
                            inquireOrderListResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrderListResult.getCreateTime()));
                        } else if (InquireOrderStatusEnum.BUYERGCONFIRM.getCode().equals(inquireOrderListResult.getStatus())) {
                            inquireOrderListResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrderListResult.getExplicitTime()));
                        } else if (InquireOrderStatusEnum.SELLERCONFIRM.getCode().equals(inquireOrderListResult.getStatus())) {
                            inquireOrderListResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrderListResult.getSellerConfirmTime()));
                        } else if (InquireOrderStatusEnum.DELIVER.getCode().equals(inquireOrderListResult.getStatus())) {
                            inquireOrderListResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrderListResult.getDeliveryTime()));
                        }
                    }
                    //获取商品名称
                    InquireSkuContentResult inquireSkuContentResult = inquireSkuContentDao.findBySkuId(inquireOrderListResult.getSkuId());
                    if (inquireSkuContentResult != null) {
                        inquireOrderListResult.setSkuName(inquireSkuContentResult.getTitle());
                        inquireOrderListResult.setSkuCover(inquireSkuContentResult.getCover());
                        //获取分类名称
                        SkuCategory skuCategory = skuCategoryDao.findById(inquireSkuContentResult.getCategoryId());
                        if (skuCategory != null) {
                            inquireOrderListResult.setCategoryName(skuCategory.getName());
                        }
                    }
                    //获取规格
                    InquireSalesInformation inquireSalesInformation = inquireSalesInformationDao.findById(inquireOrderListResult.getInquireSalesInformationId());
                    if (inquireSalesInformation != null) {
                        inquireOrderListResult.setSpecifications(inquireSalesInformation.getSpecifications());
                        inquireOrderListResult.setUnit(inquireSalesInformation.getUnit());
                    }

                    //获取物流
                    InquireOrderDeliveryDemandQuery inquireOrderDeliveryDemandQuery = new InquireOrderDeliveryDemandQuery();
                    inquireOrderDeliveryDemandQuery.setInquireOrderId(inquireOrderListResult.getId());
                    InquireOrderDeliveryDemand demandDaoFirst = inquireOrderDeliveryDemandDao.findFirst(inquireOrderDeliveryDemandQuery);
                    if (demandDaoFirst != null) {
                        InquireOrderDeliveryDemandResult inquireOrderDeliveryDemandResult = new InquireOrderDeliveryDemandResult();
                        BeanUtilExt.copyProperties(inquireOrderDeliveryDemandResult, demandDaoFirst);
                        inquireOrderListResult.setInquireOrderDeliveryDemandResult(inquireOrderDeliveryDemandResult);
                    }
                }
                orderListResult.setSubOrderList(inquireOrderListResults);
            }
            page.setData(orderListResults);
        }
        return page;
    }

    @Override
    public InquireOrderResult getInquireOrderResult(Integer inquireOrderId) throws BusinessException {
        InquireOrderResult inquireOrderResult = new InquireOrderResult();
        ValidateBusinessException.assertIdNotNull(inquireOrderId, ExceptionEnum.ERROR_1812);
        InquireOrder inquireOrder = inquireOrderDao.findByIdForUpdate(inquireOrderId);
        ValidateBusinessException.assertNonNull(inquireOrder, ExceptionEnum.ERROR_1813);
        BeanUtilExt.copyProperties(inquireOrderResult, inquireOrder);
        Integer orderId = inquireOrder.getOrderId();
        Order order = orderDao.findById(orderId);
        ValidateBusinessException.assertNonNull(order, ExceptionEnum.ERROR_1813);
        inquireOrderResult.setOrderId(inquireOrderId);
        inquireOrderResult.setRemarksOrderId(orderId);
        inquireOrderResult.setReceiver(order.getReceiver());
        inquireOrderResult.setReceiverMobile(order.getReceiverMobile());
        inquireOrderResult.setAddress(order.getAddress());
        Sku sku = skuDao.findById(inquireOrder.getSkuId());
        if (sku != null) {
            inquireOrderResult.setSkuName(sku.getTitle());
        }
        InquireSkuContent inquireSkuContent = inquireSkuContentDao.findById(inquireOrder.getInquireSkuId());
        if (inquireSkuContent != null) {
            inquireOrderResult.setSampleLogistics(inquireSkuContent.getSampleLogistics());
            inquireOrderResult.setSkuCover(inquireSkuContent.getCover());
        }
        //获取商品详情
        InquireSalesInformation inquireSalesInformation = inquireSalesInformationDao.findById(inquireOrder.getInquireSalesInformationId());
        if (inquireSalesInformation != null) {
            InquireSalesInformationResult inquireSalesInformationResult = new InquireSalesInformationResult();
            BeanUtilExt.copyProperties(inquireSalesInformationResult, inquireSalesInformation);
            inquireOrderResult.setInquireSalesInformationResult(inquireSalesInformationResult);
        }
        //获取倒计时
        SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(inquireOrder.getSupplierId(), OrderTypeEnum.INQUIRE.getCode(), inquireOrder.getStatus());
        if (supplierSystemDeploy != null) {
            if (InquireOrderStatusEnum.WAITINGCONFIRM.getCode().equals(inquireOrder.getStatus())) {
                inquireOrderResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrder.getCreateTime()));
            } else if (InquireOrderStatusEnum.BUYERGCONFIRM.getCode().equals(inquireOrder.getStatus())) {
                inquireOrderResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrder.getExplicitTime()));
            } else if (InquireOrderStatusEnum.SELLERCONFIRM.getCode().equals(inquireOrder.getStatus())) {
                inquireOrderResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrder.getSellerConfirmTime()));
            } else if (InquireOrderStatusEnum.DELIVER.getCode().equals(inquireOrder.getStatus())) {
                inquireOrderResult.setExpirationTime(addExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails(), inquireOrder.getDeliveryTime()));
            }
        }
        //获取检测机构列表
        InquireSkuTestingMessageQuery inquireSkuTestingMessageQuery = new InquireSkuTestingMessageQuery();
        inquireSkuTestingMessageQuery.setInquireSkuId(inquireOrder.getInquireSkuId());
        inquireSkuTestingMessageQuery.setIsFree("Y");
        InquireSkuTestingMessage inquireSkuTestingMessage = inquireSkuTestingMessageDao.findFirst(inquireSkuTestingMessageQuery);
        if (inquireSkuTestingMessage != null) {
            InquireSkuTestingMessageResult inquireSkuTestingMessageResult = new InquireSkuTestingMessageResult();
            BeanUtilExt.copyProperties(inquireSkuTestingMessageResult, inquireSkuTestingMessage);
            inquireOrderResult.setInquireSkuTestingMessageResult(inquireSkuTestingMessageResult);
        }

        if (OrderTypeEnum.SAMPLE.getCode().equals(inquireOrderResult.getType()) && inquireOrderResult.getOutOrderId() != null) {
            InquireOrderPlan inquireOrderPlan = inquireOrderPlanDao.findById(inquireOrderResult.getOutOrderId());
            if (inquireOrderPlan != null) {
                InquireOrderPlanResult inquireOrderPlanResult = new InquireOrderPlanResult();
                BeanUtils.copyProperties(inquireOrderPlanResult, inquireOrderPlan);
                inquireOrderResult.setSampleInquireOrderPlanResult(inquireOrderPlanResult);
            }
        }

        //意向报价信息
        InquireOrderPlanQuery inquireOrderPlanQuery = new InquireOrderPlanQuery();
        inquireOrderPlanQuery.setInquireOrderId(inquireOrderId);
        inquireOrderPlanQuery.setType(InquireOrderPlanTypeEnum.INTENTION.getCode());
        List<InquireOrderPlan> inquireOrderPlans = inquireOrderPlanDao.list(inquireOrderPlanQuery);
        if (inquireOrderPlans != null && inquireOrderPlans.size() > 0) {
            List<InquireOrderPlanResult> inquireOrderPlanResults = DTOUtils.convertList(inquireOrderPlans, InquireOrderPlanResult.class);
            for (InquireOrderPlanResult inquireOrderPlanResult : inquireOrderPlanResults) {
                InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
                inquireOrderQuery.setType(OrderTypeEnum.SAMPLE.getCode());
                inquireOrderQuery.setOutOrderId(inquireOrderPlanResult.getId());
                InquireOrder inquireOrderDaoFirst = inquireOrderDao.findFirst(inquireOrderQuery);
                if (inquireOrderDaoFirst != null) {
                    inquireOrderPlanResult.setIsHavingSample(true);
                }
            }
            inquireOrderResult.setIntentionList(inquireOrderPlanResults);
        }

        //明确报价信息
        InquireOrderPlanQuery inquireOrderPlanExplicitQuery = new InquireOrderPlanQuery();
        inquireOrderPlanExplicitQuery.setInquireOrderId(inquireOrderId);
        inquireOrderPlanExplicitQuery.setType(InquireOrderPlanTypeEnum.EXPLICIT.getCode());
        List<InquireOrderPlan> inquireOrderPlanList = inquireOrderPlanDao.list(inquireOrderPlanExplicitQuery);
        if (inquireOrderPlanList != null && inquireOrderPlanList.size() > 0) {
            inquireOrderResult.setExplicitList(DTOUtils.convertList(inquireOrderPlanList, InquireOrderPlanResult.class));
        }

        //用户确认报价信息
        InquireBuyerConfirmPlanQuery inquireBuyerConfirmPlanQuery = new InquireBuyerConfirmPlanQuery();
        inquireBuyerConfirmPlanQuery.setInquireOrderId(inquireOrderId);
        List<InquireBuyerConfirmPlan> inquireBuyerConfirmPlans = inquireBuyerConfirmPlanDao.list(inquireBuyerConfirmPlanQuery);
        if (inquireBuyerConfirmPlans != null && inquireBuyerConfirmPlans.size() > 0) {
            inquireOrderResult.setMyList(DTOUtils.convertList(inquireBuyerConfirmPlans, InquireBuyerConfirmPlanResult.class));
        }

        //支付凭证信息
        InquireOrderContractQuery inquireOrderContractQuery = new InquireOrderContractQuery();
        inquireOrderContractQuery.setInquireOrderId(inquireOrderId);
        List<InquireOrderContract> inquireOrderContracts = inquireOrderContractDao.list(inquireOrderContractQuery);
        if (inquireOrderContracts != null && inquireOrderContracts.size() > 0) {
            inquireOrderResult.setContractResults(DTOUtils.convertList(inquireOrderContracts, InquireOrderContractResult.class));
        }
        InquireOrderTestingQuery inquireOrderTestingQuery = new InquireOrderTestingQuery();
        inquireOrderTestingQuery.setInquireOrderId(inquireOrderId);
        InquireOrderTesting inquireOrderTesting = inquireOrderTestingDao.findFirst(inquireOrderTestingQuery);
        if (inquireOrderTesting != null) {
            InquireOrderTestingResult inquireOrderTestingResult = new InquireOrderTestingResult();
            BeanUtilExt.copyProperties(inquireOrderTestingResult, inquireOrderTesting);
            inquireOrderResult.setInquireOrderTestingResult(inquireOrderTestingResult);
        }
        List<Integer> inquireOrderIdByOrderId = inquireOrderDao.findInquireOrderIdByOrderId(orderId);
        //物流订单
        List<InquireOrderDeliveryDemand> inquireOrderIds = inquireOrderDeliveryDemandDao.findByInquireOrderIds(inquireOrderIdByOrderId);
        if (inquireOrderIds != null && inquireOrderIds.size() > 0) {
            List<InquireOrderDeliveryDemandResult> inquireOrderDeliveryDemandResults = DTOUtils.convertList(inquireOrderIds, InquireOrderDeliveryDemandResult.class);
            for (InquireOrderDeliveryDemandResult inquireOrderDeliveryDemandResult : inquireOrderDeliveryDemandResults) {
                if (inquireOrderDeliveryDemandResult.getInquireOrderId().equals(inquireOrderId)) {
                    inquireOrderDeliveryDemandResult.setType(OrderTypeEnum.INQUIRE.getCode());
                } else {
                    inquireOrderDeliveryDemandResult.setType(OrderTypeEnum.SAMPLE.getCode());
                }
            }
            inquireOrderResult.setInquireOrderDeliveryDemandResult(inquireOrderDeliveryDemandResults);
        }
        inquireOrderIdByOrderId.stream().findFirst().map(vo -> {
            if (vo.equals(inquireOrderId)) {
                inquireOrderIdByOrderId.remove(vo);
            }
            return vo;
        });
        inquireOrderResult.setSampleOrderId(inquireOrderIdByOrderId);
        return inquireOrderResult;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Void notifyRecharge(Integer id, String billNo, Date payTime, Map<String, String> map) throws BusinessException {
        LOG.info("notifyRecharge===" + id);
        PayOrder payOrder = payOrderDao.findByIdForUpdate(id);
        if ("Y".equals(payOrder.getIsPay())) {
            return null;
        }
        Date date = new Date();
        PayOrder param = new PayOrder();
        param.setIsPay("Y");
        param.setPayTime(date);
        param.setBillNo(billNo);
        param.setId(id);
        payOrderDao.updateById(param);
        Integer orderId = Integer.parseInt(payOrder.getOutOrderId().split("-")[1]);
        if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.CAR.getCode())) {
            notifyCarOrder(orderId, date);
        } else if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.SKU.getCode())) {
            notifySkuOrder(orderId, date);
        } else if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.LOGISTICS.getCode())) {
            notifySkuOrderDeliveryDemand(orderId, date);
        } else if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.INQUIRELOGISTICS.getCode())) {
            notifyInquireOrderDeliveryDemand(orderId, date);
        } else if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.TESTING.getCode())) {
            notifyTestingOrder(orderId, date);
        } else if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.ENJOY.getCode())) {
            notifyEnjoyOrder(payOrder.getId(), date);
        } else if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.PERIODIZATION.getCode())) {
            notifyPeriodizationOrder(orderId, date);
        } else if (payOrder.getOutOrderId().startsWith(environment + OrderTypeEnum.BALANCE.getCode())) {
            notifyBalanceOrder(payOrder.getId(), orderId, date);
        }

        return null;
    }

    @Override
    public void payContinueByBalance(OrderResult orderResult) throws BusinessException {
        Order order = orderDao.findById(orderResult.getId());
        Integer deleteBalance = merchantDao.deleteBalance(orderResult.getMerchantId(), orderResult.getPayPrice());
        Date date = new Date();
        if (deleteBalance > 0) {
            OrderBalance orderBalance = new OrderBalance();
            orderBalance.setMerchantId(orderResult.getMerchantId());
            orderBalance.setPayOrderId(order.getId());
            orderBalance.setBalanceMoney(order.getPayPrice());
            orderBalance.setRechargeType("shopping");
            orderBalance.setNotes("购买商品，订单id:" + order.getId());
            orderBalance.setStatus(SkuOrderStatusEnum.SUCCESS.getCode());
            orderBalance.setCreateTime(date);
            orderBalanceDao.insert(orderBalance);
            Integer orderId = Integer.parseInt(orderResult.getOutOrderId().split("-")[1]);
            if (orderResult.getOutOrderId().startsWith(environment + OrderTypeEnum.CAR.getCode())) {
                notifyCarOrder(orderId, date);
            } else if (orderResult.getOutOrderId().startsWith(environment + OrderTypeEnum.SKU.getCode())) {
                notifySkuOrder(orderId, date);
            } else if (orderResult.getOutOrderId().startsWith(environment + OrderTypeEnum.ENJOY.getCode())) {
                OrderRelationQuery query = new OrderRelationQuery();
                query.setSkuOrderId(orderId);
                List<OrderRelation> list = orderRelationDao.list(query);
                for (OrderRelation orderRelation : list) {
                    SkuOrder newBO = new SkuOrder();
                    newBO.setIsPay("Y");
                    newBO.setPayTime(date);
                    newBO.setId(orderRelation.getSkuOrderId());
                    skuOrderDao.updateById(newBO);
                    SkuOrderDeliveryDemand skuOrderDeliveryDemand = new SkuOrderDeliveryDemand();
                    skuOrderDeliveryDemand.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
                    skuOrderDeliveryDemand.setSkuOrderId(orderRelation.getSkuOrderId());
                    skuOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.PAY.getCode());
                    skuOrderDeliveryDemand.setPostagePayTime(date);
                    skuOrderDeliveryDemandDao.updateBySkuOrderId(skuOrderDeliveryDemand);
                }
            } else if (orderResult.getOutOrderId().startsWith(environment + OrderTypeEnum.PERIODIZATION.getCode())) {
                notifyPeriodizationOrder(orderId, date);
            }
        }
    }

    @Override
    public Page<SupplierSubOrderListResult> pageSupplierSubOrderListResult(SupplierOrderListReq req, PagerInfo pagerInfo) {
        Page<SupplierSubOrderListResult> page = new Page<>(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
        req.setType(OrderTypeEnum.INQUIRE.getCode());
        Integer integer = inquireOrderDao.pageSupplierOrderCount(req);
        page.setTotalPage(integer);
        if (integer > 0) {
            List<SupplierSubOrderListResult> supplierSubOrderListResults = inquireOrderDao.pageSupplierSubOrderListResult(req, pagerInfo.getStart(), pagerInfo.getPageSize());
            List<Integer> orderIds = supplierSubOrderListResults.stream().map(SupplierSubOrderListResult::getOrderId).collect(Collectors.toList());
            List<InquireOrder> supplierSubOrderListByOrderIdList = inquireOrderDao.getSupplierSubOrderListByOrderIdList(orderIds);
            Map<Integer, List<InquireOrder>> integerListMap = supplierSubOrderListByOrderIdList.stream().collect(Collectors.groupingBy(InquireOrder::getOrderId));
            for (SupplierSubOrderListResult supplierSubOrderListResult : supplierSubOrderListResults) {
                List<InquireOrder> inquireOrders = integerListMap.get(supplierSubOrderListResult.getOrderId());
                supplierSubOrderListResult.setIsHavingSample(inquireOrders.size() > 1 ? "Y" : "N");
                InquireOrderDeliveryDemandQuery inquireOrderDeliveryDemandQuery = new InquireOrderDeliveryDemandQuery();
                inquireOrderDeliveryDemandQuery.setInquireOrderId(supplierSubOrderListResult.getInquireOrderId());
                InquireOrderDeliveryDemand inquireOrderDeliveryDemand = inquireOrderDeliveryDemandDao.findFirst(inquireOrderDeliveryDemandQuery);
                if (inquireOrderDeliveryDemand != null) {
                    supplierSubOrderListResult.setPostageStatus(inquireOrderDeliveryDemand.getPostageStatus());
                }
            }
            page.setData(supplierSubOrderListResults);
        }
        return page;
    }

    @Override
    public SupplierOrderResult getSupplierOrderResult(Integer inquireOrderId, Integer supplierId, String roleType) throws BusinessException {
        SupplierOrderResult supplierOrderResult = new SupplierOrderResult();
        InquireOrderResult inquireOrderResult = this.getInquireOrderResult(inquireOrderId);
        if (RoleTypeEnum.SUPPLIER.getCode().equals(roleType)) {
            ValidateBusinessException.assertTrue(supplierId.equals(inquireOrderResult.getSupplierId()), ExceptionEnum.ERROR_1815);
        }
        Integer orderId = inquireOrderResult.getRemarksOrderId();
        Order order = orderDao.findById(orderId);
        BeanUtilExt.copyProperties(supplierOrderResult, order);
        List<InquireOrderResult> inquireOrderResults = new ArrayList<>();
        inquireOrderResults.add(inquireOrderResult);
        List<Integer> inquireOrderIdByOrderId = inquireOrderDao.findInquireOrderIdByOrderId(orderId);
        inquireOrderIdByOrderId.remove(inquireOrderId);
        if (inquireOrderIdByOrderId.size() > 0) {
            for (Integer integer : inquireOrderIdByOrderId) {
                inquireOrderResults.add(this.getInquireOrderResult(integer));
            }
        }
        supplierOrderResult.setInquireOrderResults(inquireOrderResults);
        List<InquireOrderLog> inquireOrderLogs = inquireOrderLogDao.findByOrderId(orderId, OrderTypeEnum.INQUIRE.getCode());
        if (inquireOrderLogs != null && inquireOrderLogs.size() > 0) {
            List<InquireOrderLogResult> inquireOrderLogResults = DTOUtils.convertList(inquireOrderLogs, InquireOrderLogResult.class);
            Map<String, List<InquireOrderLogResult>> stringListMap = inquireOrderLogResults.stream().collect(Collectors.groupingBy(InquireOrderLogResult::getDescribePeople));
            supplierOrderResult.setInquireOrderLogResults(stringListMap);
        }
        return supplierOrderResult;
    }

    @Override
    public void cancelOrder(Integer inquireOrderId, Integer userId, String userType, String cancelReason) throws BusinessException {
        InquireOrderResult inquireOrderResult = this.getInquireOrderResult(inquireOrderId);
        if (RoleTypeEnum.MERCHANT.getCode().equals(userType)) {
            ValidateBusinessException.assertTrue(userId.equals(inquireOrderResult.getMerchantId()), ExceptionEnum.ERROR_1815);
        } else if (RoleTypeEnum.SUPPLIER.getCode().equals(userType)) {
            ValidateBusinessException.assertTrue(userId.equals(inquireOrderResult.getSupplierId()), ExceptionEnum.ERROR_1815);
        }
        InquireOrder updateInquireOrder = new InquireOrder();
        updateInquireOrder.setId(inquireOrderId);
        updateInquireOrder.setStatus(InquireOrderStatusEnum.CANCEL.getCode());
        updateInquireOrder.setCancelReason(cancelReason);
        inquireOrderDao.updateById(updateInquireOrder);
        if (RoleTypeEnum.MERCHANT.getCode().equals(userType)) {
            Merchant merchant = merchantDao.findById(userId);
            addInquireOrderLog(inquireOrderResult.getRemarksOrderId(), "取消订单", "买家", merchant.getName());
        } else if (RoleTypeEnum.SUPPLIER.getCode().equals(userType)) {
            Supplier supplier = supplierDao.findById(userId);
            addInquireOrderLog(inquireOrderResult.getRemarksOrderId(), "取消订单", "卖方", supplier.getName());
        }
    }

    @Override
    public void confirmOrder(Integer inquireOrderId, Integer userId, String userType) throws BusinessException {
        InquireOrderResult inquireOrderResult = this.getInquireOrderResult(inquireOrderId);
        if (RoleTypeEnum.MERCHANT.getCode().equals(userType)) {
            ValidateBusinessException.assertTrue(userId.equals(inquireOrderResult.getMerchantId()), ExceptionEnum.ERROR_1815);
        } else if (RoleTypeEnum.SUPPLIER.getCode().equals(userType)) {
            ValidateBusinessException.assertTrue(userId.equals(inquireOrderResult.getSupplierId()), ExceptionEnum.ERROR_1815);
        }
        ValidateBusinessException.assertTrue(InquireOrderStatusEnum.DELIVER.getCode().equals(inquireOrderResult.getStatus()), ExceptionEnum.ERROR_1815);
        InquireOrder updateInquireOrder = new InquireOrder();
        updateInquireOrder.setId(inquireOrderId);
        updateInquireOrder.setStatus(InquireOrderStatusEnum.SUCCESS.getCode());
        updateInquireOrder.setFinishTime(new Date());
        inquireOrderDao.updateById(updateInquireOrder);
        if (RoleTypeEnum.MERCHANT.getCode().equals(userType)) {
            Merchant merchant = merchantDao.findById(userId);
            addInquireOrderLog(inquireOrderId, "确认收货", "买家", merchant.getName());
        } else if (RoleTypeEnum.SUPPLIER.getCode().equals(userType)) {
            Supplier supplier = supplierDao.findById(userId);
            addInquireOrderLog(inquireOrderId, "确认收货", "卖方", supplier.getName());
        }
    }

    @Override
    public void invoicingOrder(Integer inquireOrderId, Integer merchantId, Integer merchantInvoiceId) throws BusinessException {
        MerchantInvoiceQuery merchantInvoiceQuery = new MerchantInvoiceQuery();
        merchantInvoiceQuery.setMerchantId(merchantId);
        merchantInvoiceQuery.setId(merchantInvoiceId);
        MerchantInvoice merchantInvoice = merchantInvoiceDao.findFirst(merchantInvoiceQuery);
        ValidateBusinessException.assertNonNull(merchantInvoice, ExceptionEnum.ERROR_1863);
        InquireOrder inquireOrder = inquireOrderDao.findById(inquireOrderId);
        ValidateBusinessException.assertStringBlank(inquireOrder.getRise(), ExceptionEnum.ERROR_1864);
        InquireOrder updateInquireOrder = new InquireOrder();
        updateInquireOrder.setId(inquireOrderId);
        updateInquireOrder.setRise(merchantInvoice.getRise());
        updateInquireOrder.setDutyParagraph(merchantInvoice.getDutyParagraph());
        inquireOrderDao.updateById(updateInquireOrder);
    }

    @Override
    public void invoicingOrderByInvoicingOrderReq(InvoicingOrderReq req) throws BusinessException {
        Integer inquireOrderId = req.getInquireOrderId();
        InquireOrder inquireOrder = inquireOrderDao.findById(inquireOrderId);
        ValidateBusinessException.assertStringBlank(inquireOrder.getRise(), ExceptionEnum.ERROR_1864);
        MerchantInvoiceQuery merchantInvoiceQuery = new MerchantInvoiceQuery();
        merchantInvoiceQuery.setRise(req.getTitle());
        merchantInvoiceQuery.setMerchantId(req.getMerchantId());
        MerchantInvoice merchantInvoice = merchantInvoiceDao.findFirst(merchantInvoiceQuery);
        MerchantInvoice param = new MerchantInvoice();
        param.setDutyParagraph(req.getTaxNumber());
        param.setBankName(req.getBankName());
        param.setBankAccount(req.getBankAccount());
        param.setMobile(req.getTelephone());
        param.setAddress(req.getCompanyAddress());
        param.setIsDefault("N");
        param.setMerchantId(req.getMerchantId());
        if ("0".equals(req.getType())) {
            param.setGroupCode("企业");
        } else {
            param.setGroupCode("个人咖啡店");
        }
        if (merchantInvoice != null) {
            param.setId(merchantInvoice.getId());
            merchantInvoiceDao.updateById(param);
            InquireOrder updateInquireOrder = new InquireOrder();
            updateInquireOrder.setId(inquireOrderId);
            updateInquireOrder.setRise(param.getRise());
            updateInquireOrder.setDutyParagraph(param.getDutyParagraph());
            inquireOrderDao.updateById(updateInquireOrder);
        } else {
            param.setRise(req.getTitle());
            param.setCreateTime(new Date());
            param.setBankAccount(req.getBankAccount());
            merchantInvoiceDao.insert(param);
            InquireOrder updateInquireOrder = new InquireOrder();
            updateInquireOrder.setId(inquireOrderId);
            updateInquireOrder.setRise(param.getRise());
            updateInquireOrder.setDutyParagraph(param.getDutyParagraph());
            inquireOrderDao.updateById(updateInquireOrder);
        }
    }

    @Override
    public void cancelWaitingOrder() {
        SupplierQuery supplierQuery = new SupplierQuery();
        supplierQuery.setPid(0);
        List<Supplier> supplierList = supplierDao.page(supplierQuery, 0, Integer.MAX_VALUE);
        for (Supplier supplier : supplierList) {
            SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(supplier.getId(), OrderTypeEnum.INQUIRE.getCode(), InquireOrderStatusEnum.WAITING.getCode());
            if (supplierSystemDeploy == null) {
                continue;
            }
            InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
            inquireOrderQuery.setStatus(InquireOrderStatusEnum.WAITING.getCode());
            inquireOrderQuery.setWaitingExpireTime(getExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails()));
            List<InquireOrder> inquireOrders = inquireOrderDao.list(inquireOrderQuery);
            for (InquireOrder inquireOrder : inquireOrders) {
                InquireOrder update = new InquireOrder();
                update.setId(inquireOrder.getId());
                update.setStatus(InquireOrderStatusEnum.CANCEL.getCode());
                inquireOrderDao.updateById(update);
            }
        }
    }

    @Override
    public void cancelBuyerConfirmOrder() {
        SupplierQuery supplierQuery = new SupplierQuery();
        supplierQuery.setPid(0);
        List<Supplier> supplierList = supplierDao.page(supplierQuery, 0, Integer.MAX_VALUE);
        for (Supplier supplier : supplierList) {
            SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(supplier.getId(), OrderTypeEnum.INQUIRE.getCode(), InquireOrderStatusEnum.BUYERGCONFIRM.getCode());
            if (supplierSystemDeploy == null) {
                continue;
            }
            InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
            inquireOrderQuery.setStatus(InquireOrderStatusEnum.BUYERGCONFIRM.getCode());
            inquireOrderQuery.setBuyerConfirmExpireTime(getExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails()));
            List<InquireOrder> inquireOrders = inquireOrderDao.list(inquireOrderQuery);
            for (InquireOrder inquireOrder : inquireOrders) {
                InquireOrder update = new InquireOrder();
                update.setId(inquireOrder.getId());
                update.setStatus(InquireOrderStatusEnum.CANCEL.getCode());
                inquireOrderDao.updateById(update);
            }
        }
    }

    @Override
    public void cancelSellerConfirmOrder() {
        SupplierQuery supplierQuery = new SupplierQuery();
        supplierQuery.setPid(0);
        List<Supplier> supplierList = supplierDao.page(supplierQuery, 0, Integer.MAX_VALUE);
        for (Supplier supplier : supplierList) {
            SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(supplier.getId(), OrderTypeEnum.INQUIRE.getCode(), InquireOrderStatusEnum.SELLERCONFIRM.getCode());
            if (supplierSystemDeploy == null) {
                continue;
            }
            InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
            inquireOrderQuery.setStatus(InquireOrderStatusEnum.SELLERCONFIRM.getCode());
            inquireOrderQuery.setSellerConfirmExpireTime(getExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails()));
            List<InquireOrder> inquireOrders = inquireOrderDao.list(inquireOrderQuery);
            for (InquireOrder inquireOrder : inquireOrders) {
                InquireOrder update = new InquireOrder();
                update.setId(inquireOrder.getId());
                update.setStatus(InquireOrderStatusEnum.CANCEL.getCode());
                inquireOrderDao.updateById(update);
            }
        }
    }

    @Override
    public void transactionFinash() {
        SupplierQuery supplierQuery = new SupplierQuery();
        supplierQuery.setPid(0);
        List<Supplier> supplierList = supplierDao.page(supplierQuery, 0, Integer.MAX_VALUE);
        for (Supplier supplier : supplierList) {
            SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(supplier.getId(), OrderTypeEnum.INQUIRE.getCode(), InquireOrderStatusEnum.DELIVER.getCode());
            if (supplierSystemDeploy == null) {
                continue;
            }
            InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
            inquireOrderQuery.setStatus(InquireOrderStatusEnum.DELIVER.getCode());
            inquireOrderQuery.setDeliverExpireTime(getExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails()));
            List<InquireOrder> inquireOrders = inquireOrderDao.list(inquireOrderQuery);
            for (InquireOrder inquireOrder : inquireOrders) {
                InquireOrder update = new InquireOrder();
                update.setId(inquireOrder.getId());
                update.setStatus(InquireOrderStatusEnum.SUCCESS.getCode());
                inquireOrderDao.updateById(update);
            }
        }
    }

    @Override
    public void cancelSampleSellerConfirmOrder() {
        SupplierQuery supplierQuery = new SupplierQuery();
        supplierQuery.setPid(0);
        List<Supplier> supplierList = supplierDao.page(supplierQuery, 0, Integer.MAX_VALUE);
        for (Supplier supplier : supplierList) {
            SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(supplier.getId(), OrderTypeEnum.SAMPLE.getCode(), SkuOrderStatusEnum.WAITING.getCode());
            if (supplierSystemDeploy == null) {
                continue;
            }
            InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
            inquireOrderQuery.setStatus(SkuOrderStatusEnum.WAITING.getCode());
            inquireOrderQuery.setDeliverExpireTime(getExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails()));
            List<InquireOrder> inquireOrders = inquireOrderDao.list(inquireOrderQuery);
            for (InquireOrder inquireOrder : inquireOrders) {
                InquireOrder update = new InquireOrder();
                update.setId(inquireOrder.getId());
                update.setStatus(SkuOrderStatusEnum.CANCEL.getCode());
                inquireOrderDao.updateById(update);
            }
        }
    }

    @Override
    public void sampleTransactionFinash() {
        SupplierQuery supplierQuery = new SupplierQuery();
        supplierQuery.setPid(0);
        List<Supplier> supplierList = supplierDao.page(supplierQuery, 0, Integer.MAX_VALUE);
        for (Supplier supplier : supplierList) {
            SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(supplier.getId(), OrderTypeEnum.SAMPLE.getCode(), InquireOrderStatusEnum.DELIVER.getCode());
            if (supplierSystemDeploy == null) {
                continue;
            }
            InquireOrderQuery inquireOrderQuery = new InquireOrderQuery();
            inquireOrderQuery.setStatus(InquireOrderStatusEnum.DELIVER.getCode());
            inquireOrderQuery.setDeliverExpireTime(getExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails()));
            List<InquireOrder> inquireOrders = inquireOrderDao.list(inquireOrderQuery);
            for (InquireOrder inquireOrder : inquireOrders) {
                InquireOrder update = new InquireOrder();
                update.setId(inquireOrder.getId());
                update.setStatus(InquireOrderStatusEnum.SUCCESS.getCode());
                inquireOrderDao.updateById(update);
            }
        }
    }

    @Override
    public void cancelTestingOrder() {
        SupplierQuery supplierQuery = new SupplierQuery();
        supplierQuery.setPid(0);
        List<Supplier> supplierList = supplierDao.page(supplierQuery, 0, Integer.MAX_VALUE);
        for (Supplier supplier : supplierList) {
            SupplierSystemDeploy supplierSystemDeploy = getSupplierSystemDeploy(supplier.getId(), OrderTypeEnum.TESTING.getCode(), SkuOrderStatusEnum.WAITING.getCode());
            if (supplierSystemDeploy == null) {
                continue;
            }
            InquireOrderTestingQuery inquireOrderTestingQuery = new InquireOrderTestingQuery();
            inquireOrderTestingQuery.setTestingStatus(SkuOrderStatusEnum.WAITING.getCode());
            inquireOrderTestingQuery.setWaitingExpireTime(getExpireTime(supplierSystemDeploy.getUnit(), supplierSystemDeploy.getDetails()));
            List<InquireOrderTesting> inquireOrderTestings = inquireOrderTestingDao.list(inquireOrderTestingQuery);
            for (InquireOrderTesting inquireOrderTesting : inquireOrderTestings) {
                InquireOrderTesting update = new InquireOrderTesting();
                update.setId(inquireOrderTesting.getId());
                update.setTestingResult(InquireOrderStatusEnum.SUCCESS.getCode());
                inquireOrderTestingDao.updateById(update);
            }
        }
    }

    SupplierSystemDeploy getSupplierSystemDeploy(Integer supplierId, String type, String status) {
        SupplierSystemDeployQuery supplierSystemDeployQuery = new SupplierSystemDeployQuery();
        supplierSystemDeployQuery.setSupplierId(supplierId);
        supplierSystemDeployQuery.setType(type);
        supplierSystemDeployQuery.setStatus(status);
        SupplierSystemDeploy supplierSystemDeploy = supplierSystemDeployDao.findFirst(supplierSystemDeployQuery);
        return supplierSystemDeploy;
    }


    public Date getExpireTime(String uint, Integer details) {
        if (SystemDeployTimeEnum.DAY.getCode().equals(uint)) {
            return DateUtil.addDays(new Date(), -details);
        } else if (SystemDeployTimeEnum.HOUR.getCode().equals(uint)) {
            return DateUtil.addHour(new Date(), -details);
        } else if (SystemDeployTimeEnum.HOUR.getCode().equals(uint)) {
            return DateUtil.addHour(new Date(), -details);
        } else if (SystemDeployTimeEnum.MINUTE.getCode().equals(uint)) {
            return DateUtil.addMinutes(new Date(), -details);
        }
        return null;
    }


    public Date addExpireTime(String uint, Integer details, Date date) {
        if (SystemDeployTimeEnum.DAY.getCode().equals(uint)) {
            return DateUtil.addDays(date, details);
        } else if (SystemDeployTimeEnum.HOUR.getCode().equals(uint)) {
            return DateUtil.addHour(date, details);
        } else if (SystemDeployTimeEnum.HOUR.getCode().equals(uint)) {
            return DateUtil.addHour(date, details);
        } else if (SystemDeployTimeEnum.MINUTE.getCode().equals(uint)) {
            return DateUtil.addMinutes(date, details);
        }
        return null;
    }


    public Void notifyPeriodizationOrder(Integer id, Date payTime) throws BusinessException {
        SkuOrder skuOrder = skuOrderDao.findById(id);
        SkuOrder newBO = new SkuOrder();
        if (skuOrder.getPeriodsNumber() == 0) {
            newBO.setStatus(SkuOrderStatusEnum.SUCCESS.getCode());
            newBO.setFinishTime(new Date());
        } else {
            newBO.setStatus(SkuOrderStatusEnum.PAY.getCode());
        }
        newBO.setPayTime(payTime);
        newBO.setId(id);
        newBO.setIsPay("Y");
        skuOrderDao.updateById(newBO);
        SkuOrderDetailQuery param = new SkuOrderDetailQuery();
        param.setSkuOrderId(id);
        List<SkuOrderDetail> skuOrderDetails = skuOrderDetailDao.list(param);
        for (SkuOrderDetail skuOrderDetail : skuOrderDetails) {
            PeriodizationSku periodizationSku = periodizationSkuDao.findBySkuId(skuOrderDetail.getSkuId());
            if (periodizationSku != null) {
                SkuOrderDetailPeriodizationQuery skuOrderDetailPeriodizationQuery = new SkuOrderDetailPeriodizationQuery();
                skuOrderDetailPeriodizationQuery.setSkuOrderDetailId(skuOrderDetail.getId());
                SkuOrderDetailPeriodization skuOrderDetailPeriodization = skuOrderDetailPeriodizationDao.findFirst(skuOrderDetailPeriodizationQuery);
                if (skuOrderDetailPeriodization != null && skuOrderDetailPeriodization.getResidueSpecifications() == 0) {
                    Order order = new Order();
                    order.setId(skuOrder.getOrderId());
                    order.setStatus(SkuOrderStatusEnum.SUCCESS.getCode());
                    orderDao.updateById(order);
                }
            }
        }
        SkuOrderDeliveryDemand skuOrderDeliveryDemand = new SkuOrderDeliveryDemand();
        skuOrderDeliveryDemand.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
        skuOrderDeliveryDemand.setSkuOrderId(skuOrder.getId());
        skuOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.PAY.getCode());
        skuOrderDeliveryDemand.setPostagePayTime(new Date());
        skuOrderDeliveryDemandDao.updateBySkuOrderId(skuOrderDeliveryDemand);

        return null;
    }

    public void notifyBalanceOrder(Integer payOrderId, Integer id, Date payTime) throws BusinessException {
        OrderBalance orderBalance = orderBalanceDao.findById(id);
        if (orderBalance != null) {
            if (SkuOrderStatusEnum.SUCCESS.getCode().equals(orderBalance.getStatus())) {
                throw new BusinessException(ExceptionEnum.ERROR_2101.getCode(), ExceptionEnum.ERROR_2101.getMessage());
            } else {
                OrderBalance updateOrderbalance = new OrderBalance();
                updateOrderbalance.setId(id);
                updateOrderbalance.setPayOrderId(payOrderId);
                updateOrderbalance.setStatus(SkuOrderStatusEnum.SUCCESS.getCode());
                updateOrderbalance.setPayTime(payTime);
                Integer count = orderBalanceDao.updateById(updateOrderbalance);
                if (count > 0) {
                    merchantDao.addBalance(orderBalance.getMerchantId(), orderBalance.getBalanceMoney());
                    OrderBalance updateGift=new OrderBalance();
                    updateGift.setOrderBalanceId(id);
                    updateGift.setStatus(SkuOrderStatusEnum.SUCCESS.getCode());
                    updateGift.setPayTime(payTime);
                    Integer updateByOrderBalanceId = orderBalanceDao.updateByOrderBalanceId(updateGift);
                    if (updateByOrderBalanceId>0){
                        OrderBalance giftBalance = orderBalanceDao.findByOrderBalanceId(id);
                        merchantDao.addBalance(orderBalance.getMerchantId(), giftBalance.getBalanceMoney());
                    }
                }
            }
        }
    }


    public Void notifyCarOrder(Integer id, Date payTime) throws BusinessException {
        SkuOrder newBO = new SkuOrder();
        newBO.setStatus(SkuOrderStatusEnum.PAY.getCode());
        newBO.setPayTime(payTime);
        newBO.setOrderId(id);
        newBO.setIsPay("Y");
        skuOrderDao.updateByOrderId(newBO);
        List<SkuOrder> skuOrders = skuOrderDao.getListByOrderId(InquireOrderStatusEnum.CANCEL.getCode(), id);
        LOG.info("skuOrders===" + JSONObject.toJSONString(skuOrders));
        for (SkuOrder skuOrder : skuOrders) {
            SkuOrderDetailQuery param = new SkuOrderDetailQuery();
            param.setSkuOrderId(skuOrder.getId());
            List<SkuOrderDetail> skuOrderDetails = skuOrderDetailDao.list(param);
            LOG.info("skuOrderDetails===" + JSONObject.toJSONString(skuOrderDetails));
            for (SkuOrderDetail skuOrderDetail : skuOrderDetails) {
                Integer integer = skuSalesInformationDao.deleteStock(skuOrderDetail.getSkuSalesInformationId(), skuOrderDetail.getCount());
                LOG.info("integer===" + JSONObject.toJSONString(integer));
                ValidateBusinessException.assertTrue(integer > 0, ExceptionEnum.ERROR_1826.getCode(), skuOrderDetail.getSkuSalesInformationId() + "," + ExceptionEnum.ERROR_1826.getMessage());
                //积分大于0才去扣减
                if (skuOrderDetail.getIntegral() > 0) {
                    Merchant before = merchantDao.findById(skuOrder.getMerchantId());
                    LOG.info("getIntegral===" + skuOrderDetail.getIntegral() + ",before===" + before);
                    Integer integer1 = merchantDao.deleteIntegral(skuOrderDetail.getMerchantId(), skuOrderDetail.getIntegral());
                    LOG.info("integer1===" + JSONObject.toJSONString(integer1));
                    Merchant after = merchantDao.findById(skuOrder.getMerchantId());
                    ValidateBusinessException.assertTrue(integer1 > 0, ExceptionEnum.ERROR_1827.getCode(), skuOrderDetail.getSkuSalesInformationId() + "," + ExceptionEnum.ERROR_1827.getMessage());
                    MerchantIntegralLog merchantIntegralLog = new MerchantIntegralLog();
                    merchantIntegralLog.setMerchantId(skuOrder.getMerchantId());
                    merchantIntegralLog.setOperation(IntegralOperationEnum.DEDUCTION.getCode());
                    merchantIntegralLog.setBeforeIntegral(before.getIntegral());
                    merchantIntegralLog.setAfterIntegral(after.getIntegral());
                    merchantIntegralLog.setCreateTime(new Date());
                    merchantIntegralLogDao.insert(merchantIntegralLog);
                }
                SkuOrderDeliveryDemand skuOrderDeliveryDemand = new SkuOrderDeliveryDemand();
                skuOrderDeliveryDemand.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
                skuOrderDeliveryDemand.setSkuOrderId(skuOrder.getId());
                skuOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.PAY.getCode());
                skuOrderDeliveryDemand.setPostagePayTime(new Date());
                skuOrderDeliveryDemandDao.updateBySkuOrderId(skuOrderDeliveryDemand);
            }


        }
        return null;
    }

    public Void notifySkuOrder(Integer id, Date payTime) throws BusinessException {
        SkuOrder skuOrder = skuOrderDao.findById(id);
        SkuOrder newBO = new SkuOrder();
        newBO.setStatus(SkuOrderStatusEnum.PAY.getCode());
        newBO.setPayTime(payTime);
        newBO.setId(id);
        newBO.setIsPay("Y");
        skuOrderDao.updateById(newBO);
        SkuOrderDetailQuery param = new SkuOrderDetailQuery();
        param.setSkuOrderId(id);
        List<SkuOrderDetail> skuOrderDetails = skuOrderDetailDao.list(param);
        for (SkuOrderDetail skuOrderDetail : skuOrderDetails) {
            Integer integer = skuSalesInformationDao.deleteStock(skuOrderDetail.getSkuSalesInformationId(), skuOrderDetail.getCount());
            ValidateBusinessException.assertTrue(integer > 0, ExceptionEnum.ERROR_1826.getCode(), skuOrderDetail.getSkuSalesInformationId() + "," + ExceptionEnum.ERROR_1826.getMessage());
            if (StringUtils.isNotEmpty(skuOrder.getPartSn())) {
                SkuOrderQuery skuOrderQuery = new SkuOrderQuery();
                skuOrderQuery.setPartSn(skuOrder.getPartSn());
                skuOrderQuery.setNoStatus(SkuOrderStatusEnum.WAITING.getCode());
                List<SkuOrder> list = skuOrderDao.list(skuOrderQuery);
                SkuContent skuContent = skuContentDao.findBySkuId(skuOrderDetail.getSkuId());
                if (list.size() >= skuContent.getSpellOrderPeople()) {
                    skuOrderDao.updateByPartSn(skuOrder.getPartSn());
                }
            }
        }

        if (skuOrder.getIntegral() > 0) {
            Merchant before = merchantDao.findById(skuOrder.getMerchantId());
            Integer integer1 = merchantDao.deleteIntegral(skuOrder.getMerchantId(), skuOrder.getIntegral());
            Merchant after = merchantDao.findById(skuOrder.getMerchantId());
            ValidateBusinessException.assertTrue(integer1 > 0, ExceptionEnum.ERROR_1827.getCode(), skuOrder.getId() + "," + ExceptionEnum.ERROR_1827.getMessage());
            MerchantIntegralLog merchantIntegralLog = new MerchantIntegralLog();
            merchantIntegralLog.setMerchantId(skuOrder.getMerchantId());
            merchantIntegralLog.setOperation(IntegralOperationEnum.DEDUCTION.getCode());
            merchantIntegralLog.setBeforeIntegral(before.getIntegral());
            merchantIntegralLog.setAfterIntegral(after.getIntegral());
            merchantIntegralLog.setCreateTime(new Date());
            merchantIntegralLogDao.insert(merchantIntegralLog);
        }
        SkuOrderDeliveryDemand skuOrderDeliveryDemand = new SkuOrderDeliveryDemand();
        skuOrderDeliveryDemand.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
        skuOrderDeliveryDemand.setSkuOrderId(skuOrder.getId());
        skuOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.PAY.getCode());
        skuOrderDeliveryDemand.setPostagePayTime(new Date());
        skuOrderDeliveryDemandDao.updateBySkuOrderId(skuOrderDeliveryDemand);

        return null;
    }

    public Void notifySkuOrderDeliveryDemand(Integer id, Date payTime) throws BusinessException {
        SkuOrderDeliveryDemand param = new SkuOrderDeliveryDemand();
        param.setId(id);
        param.setPostageStatus(SkuOrderStatusEnum.PAY.getCode());
        param.setPostagePayTime(payTime);
        skuOrderDeliveryDemandDao.updateById(param);
        return null;
    }

    public Void notifyInquireOrderDeliveryDemand(Integer id, Date payTime) throws BusinessException {
        InquireOrderDeliveryDemand param = new InquireOrderDeliveryDemand();
        param.setId(id);
        param.setPostageStatus(SkuOrderStatusEnum.PAY.getCode());
        param.setPostagePayTime(payTime);
        inquireOrderDeliveryDemandDao.updateById(param);
        return null;
    }

    public Void notifyTestingOrder(Integer id, Date payTime) throws BusinessException {
        InquireOrderTesting param = new InquireOrderTesting();
        param.setId(id);
        param.setTestingStatus(SkuOrderStatusEnum.PAY.getCode());
        param.setTestingPayTime(payTime);
        inquireOrderTestingDao.updateById(param);
        return null;
    }

    public Void notifyEnjoyOrder(Integer id, Date payTime) throws BusinessException {
        OrderRelationQuery query = new OrderRelationQuery();
        query.setPayOrderId(id);
        List<OrderRelation> list = orderRelationDao.list(query);
        for (OrderRelation orderRelation : list) {
            SkuOrder newBO = new SkuOrder();
            newBO.setIsPay("Y");
            newBO.setPayTime(payTime);
            newBO.setId(orderRelation.getSkuOrderId());
            skuOrderDao.updateById(newBO);
            SkuOrderDeliveryDemand skuOrderDeliveryDemand = new SkuOrderDeliveryDemand();
            skuOrderDeliveryDemand.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
            skuOrderDeliveryDemand.setSkuOrderId(orderRelation.getSkuOrderId());
            skuOrderDeliveryDemand.setPostageStatus(SkuOrderStatusEnum.PAY.getCode());
            skuOrderDeliveryDemand.setPostagePayTime(new Date());
            skuOrderDeliveryDemandDao.updateBySkuOrderId(skuOrderDeliveryDemand);
        }
        return null;
    }

    MerchantCity getMerchantCity(Integer merchantCityId) {
        MerchantCity merchantCity = merchantCityDao.findById(merchantCityId);
        if (merchantCity != null) {
            if (merchantCity.getProvinceId() != null) {
                GtRegion province = gtRegionDao.findById(merchantCity.getProvinceId());
                if (province != null)
                    merchantCity.setProvinceName(province.getName());
            }
            if (merchantCity.getCityId() != null) {
                GtRegion city = gtRegionDao.findById(merchantCity.getCityId());
                if (city != null)
                    merchantCity.setCityName(city.getName());
            }
            if (merchantCity.getAreaId() != null) {
                GtRegion area = gtRegionDao.findById(merchantCity.getAreaId());
                if (area != null)
                    merchantCity.setAreaName(area.getName());
            }
            return merchantCity;
        } else {
            return null;
        }
    }
}
